<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
      class MyPromise {
        constructor (fn) {
          this.state = 'pending'
          this.value = undefined
          this.reason = undefined

          let resolve = value => {
            if (this.state === 'pending') {
              this.state = 'fulfilled'
              this.value = value
            }
          }

          let reject = reason => {
            if (this.state === 'pending') {
              this.state = 'reject'
              this.reason = reason
            }
          }

          try {
            fn(resolve, reject)
          } catch(err) {
            reject(err)
          }

        }
        then(onFulfilled, onRejected) {
            if(this.state === 'fulfilled') {
              onFulfilled(this.value)
            } else {
              onRejected(this.reason)
            }
        }
      }

    //   new MyPromise((resolve, reject) => {
    //     resolve(10)
    //   }).then((a) => {
    //     console.log('fulfilled', a)
    //   }, () => {
    //     console.log('err')
    //   })

    let p = new MyPromise((resolve, reject) => {
      resolve()
    })
    // let p1 = new Promise((resolve, reject) => {
    //   resolve()
    // })

    </script>
</body>
</html>